﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Namespace ClubStarterKit.Web.Controls

    Public Class PhotoNumberer
        Inherits WebControl
        Implements IPostBackEventHandler

        Private m_SelectedPage, m_Count, m_displayedPages As Integer


        Public Property SelectedPage() As Integer
            Get
                If m_SelectedPage = 0 Then
                    Dim o As Object = ViewState("SelectedPage")
                    If o Is Nothing Then m_SelectedPage = 1 Else m_SelectedPage = CInt(o)
                End If
                Return m_SelectedPage
            End Get
            Set(ByVal value As Integer)
                ViewState("SelectedPage") = value
                m_SelectedPage = value
            End Set
        End Property

        Public Property Count() As Integer
            Get
                If m_Count = 0 Then
                    Dim o As Object = ViewState("Count")
                    If o Is Nothing Then m_Count = 1 Else m_Count = CInt(o)
                End If
                Return m_Count
            End Get
            Set(ByVal value As Integer)
                ViewState("Count") = value
                m_Count = value
            End Set
        End Property

        Public Property DisplayedPages() As Integer
            Get
                If m_displayedPages = 0 Then
                    Dim o As Object = ViewState("DisplayedPages")
                    If o Is Nothing Then m_displayedPages = 1 Else m_displayedPages = CInt(o)
                End If
                Return m_displayedPages
            End Get
            Set(ByVal value As Integer)
                ViewState("DisplayedPages") = value
                m_displayedPages = value
            End Set
        End Property

        Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
            Get
                Return HtmlTextWriterTag.Div
            End Get
        End Property

        Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim prevListCount, nextListCount, startPage, endPage As Integer

            prevListCount = CInt(Math.Abs((m_displayedPages - 1) / 2))
            If m_SelectedPage <= prevListCount Then
                prevListCount = m_SelectedPage - 1
            End If

            nextListCount = m_displayedPages - prevListCount - 1
            If m_SelectedPage + nextListCount > m_Count Then
                nextListCount = m_Count - m_SelectedPage
            End If

            startPage = m_SelectedPage - prevListCount
            endPage = m_SelectedPage + nextListCount

            'If startPage > 1 Then
            '    renderItem(writer, "&laquo; First", 1)
            'End If

            If SelectedPage < m_Count Then renderItem(writer, "<div class='nextlink'>Next Photo &gt;</div><br />", SelectedPage + 1)

            If SelectedPage > 1 Then
                renderItem(writer, "&lt; Prev Photo", SelectedPage - 1)
            End If

            'For count As Integer = startPage To endPage
            '    Dim label As String
            '    If count <> endPage Then
            '        label = count.ToString() & " "
            '    Else
            '        label = count.ToString()
            '    End If

            '    If count = m_SelectedPage Then
            '        renderItem(writer, label, 0)
            '    Else
            '        renderItem(writer, label, count)
            '    End If
            'Next


            'If endPage < m_Count Then renderItem(writer, "Last &raquo;", m_Count)
        End Sub

        Sub renderItem(ByVal writer As HtmlTextWriter, ByVal text As String, ByVal pageNum As Integer)
            writer.RenderBeginTag(HtmlTextWriterTag.Span)

            If (pageNum <> 0) Then

                writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink(Me, pageNum.ToString()))
                writer.RenderBeginTag(HtmlTextWriterTag.A)
            End If

            writer.Write(text)

            If (pageNum <> 0) Then writer.RenderEndTag()

            writer.RenderEndTag()
        End Sub

        Public Event SelectedPageChanged As EventHandler

        Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
            Dim newPage As Integer
            If Integer.TryParse(eventArgument, newPage) Then

                Me.SelectedPage = newPage
                OnSelectedPageChanged(EventArgs.Empty)
            End If
        End Sub

        Protected Overridable Sub OnSelectedPageChanged(ByVal e As EventArgs)
            RaiseEvent SelectedPageChanged(Me, e)
        End Sub

    End Class
End Namespace
